package vista;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.TextArea;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import java.awt.FlowLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import java.awt.Insets;
import javax.swing.SwingConstants;
import java.awt.Component;
import javax.swing.JLabel;
import org.eclipse.wb.swing.FocusTraversalOnArray;

import controle.*;
import modelo.*;

import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import net.miginfocom.swing.MigLayout;

public class GUInovojogo {

		JFrame janela;
		private JTextField textField_1;
		private TextArea textArea;
		JRadioButton rdbtnNewRadioButton;
		JRadioButton rdbtnNewRadioButton_1;
		JRadioButton rdbtnNewRadioButton_2;
		JRadioButton rdbtnNewRadioButton_3;
		JRadioButton rdbtnNewRadioButton_4;
		ButtonGroup bg1;
		//private JPanel panel_3;
		private JButton btnNewButton;
		private JPanel panel_3;
		private JPanel panel_4;
		private JPanel panel_5;
		Main mainNovoJogo;
		private JLabel lblNewLabel_2;
		
		public GUInovojogo(Main m) {
			
			mainNovoJogo = m;
			mainNovoJogo.cenario = m.cenario;
			mainNovoJogo.game = m.game;
			mainNovoJogo.aberto = m.aberto;
			
			rdbtnNewRadioButton = new JRadioButton("Vocalista");
			rdbtnNewRadioButton_1 = new JRadioButton("Guitarrista");
			rdbtnNewRadioButton_2 = new JRadioButton("Baixista");
			rdbtnNewRadioButton_3 = new JRadioButton("Tecladista");
			rdbtnNewRadioButton_4 = new JRadioButton("Baterista");
			
			groupButton();
			
			//ver pq nao ta funcionando
			//procurar por isSelected pra verficar qual botao foi selecionado
			
			janela = new JFrame();
			janela.setTitle("GuitaRPG");
			janela.setVisible(true);
			janela.setExtendedState(JFrame.MAXIMIZED_BOTH);
			janela.setResizable(false);
			janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			JPanel panel = new JPanel();
			janela.getContentPane().add(panel, BorderLayout.WEST);
			panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new EmptyBorder(5, 0, 5, 0));
			panel.add(panel_1);
			panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
			
			JLabel lblNewLabel = new JLabel("Nome\r\n");
			lblNewLabel.setBorder(new EmptyBorder(5, 10, 5, 10));
			panel_1.add(lblNewLabel);
			
			textField_1 = new JTextField();
			panel_1.add(textField_1);
			textField_1.setHorizontalAlignment(SwingConstants.CENTER);
			textField_1.setName("");
			textField_1.setMaximumSize(new Dimension(250, 20));
			textField_1.setColumns(10);
			panel.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{panel_1, lblNewLabel, textField_1}));
			
			JPanel panel_2 = new JPanel();
			panel_2.setBorder(new EmptyBorder(10, 10, 10, 10));
			panel.add(panel_2);
			panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
			
			JLabel lblNewLabel_1 = new JLabel("Escolha sua classe");
			lblNewLabel_1.setBorder(new EmptyBorder(0, 0, 10, 0));
			panel_2.add(lblNewLabel_1);
			
			rdbtnNewRadioButton.setHorizontalAlignment(SwingConstants.LEFT);
			panel_2.add(rdbtnNewRadioButton);
			
			rdbtnNewRadioButton_1.setHorizontalAlignment(SwingConstants.LEFT);
			panel_2.add(rdbtnNewRadioButton_1);
			
			rdbtnNewRadioButton_2.setHorizontalAlignment(SwingConstants.LEFT);
			panel_2.add(rdbtnNewRadioButton_2);
			
			rdbtnNewRadioButton_3.setHorizontalAlignment(SwingConstants.LEFT);
			panel_2.add(rdbtnNewRadioButton_3);
			
			rdbtnNewRadioButton_4.setHorizontalAlignment(SwingConstants.LEFT);
			panel_2.add(rdbtnNewRadioButton_4);
			
			OuvinteBotaoRadio teste = new OuvinteBotaoRadio();
			
			rdbtnNewRadioButton.addActionListener(teste);
			rdbtnNewRadioButton_1.addActionListener(teste);
			rdbtnNewRadioButton_2.addActionListener(teste);
			rdbtnNewRadioButton_3.addActionListener(teste);
			rdbtnNewRadioButton_4.addActionListener(teste);
			
			panel_5 = new JPanel();
			panel_5.setBorder(new EmptyBorder(10, 10, 10, 10));
			panel.add(panel_5);
			panel_5.setLayout(new BoxLayout(panel_5, BoxLayout.X_AXIS));
			
			btnNewButton = new JButton("OK");
			btnNewButton.setMinimumSize(new Dimension(100, 100));
			btnNewButton.setMaximumSize(new Dimension(100, 100));
			btnNewButton.setPreferredSize(new Dimension(100, 100));
			OuvinteBotaoOK okay = new OuvinteBotaoOK();
			btnNewButton.addActionListener(okay);
			panel_5.add(btnNewButton);
			
			panel_4 = new JPanel();
			janela.getContentPane().add(panel_4, BorderLayout.EAST);
			panel_4.setLayout(new MigLayout("", "[380px]", "[300px][160px]"));
			
			panel_3 = new JPanel();
			FlowLayout flowLayout = (FlowLayout) panel_3.getLayout();
			panel_3.setBackground(Color.ORANGE);
			panel_4.add(panel_3, "cell 0 0,grow");
			
			lblNewLabel_2 = new JLabel();
			lblNewLabel_2.setIcon(null);
			panel_3.add(lblNewLabel_2);

						
			textArea = new TextArea();
			textArea.setEditable(false);
			panel_4.add(textArea, "cell 0 1,grow");
			
		}

		
		private void groupButton(){
			bg1 = new ButtonGroup();
			
			bg1.add(rdbtnNewRadioButton);
			bg1.add(rdbtnNewRadioButton_1);
			bg1.add(rdbtnNewRadioButton_2);
			bg1.add(rdbtnNewRadioButton_3);
			bg1.add(rdbtnNewRadioButton_4);
		}
		
		class OuvinteBotaoOK implements ActionListener{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				
				if (checaBotao() != "none" && textField_1.getText().length() != 0) {
						
					
					mainNovoJogo.game.criaPersonagem(textField_1.getText(), checaBotao());
					mainNovoJogo.aberto = true;
					
					janela.setVisible(false);
					janela.dispose();
					new GUIjogo(mainNovoJogo);

				}
			}
		}
		
		class OuvinteBotaoRadio implements ActionListener{
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				
				if (checaBotao() != "none") {
					
					if (checaBotao() == "Vocalista") {
						
						textArea.setText("Classe Vocalista");
						lblNewLabel_2.setIcon(new ImageIcon("D:\\images\\vocalista.jpg"));
					}
					else if (checaBotao() == "Guitarrista") {
						
						textArea.setText("Classe Guitarrista");
						lblNewLabel_2.setIcon(new ImageIcon("D:\\images\\guitarrista.jpg"));
					}
					else if (checaBotao() == "Baixista") {
						
						textArea.setText("Classe Baixista");
						lblNewLabel_2.setIcon(new ImageIcon("D:\\images\\baixista.jpg"));
					}
					else if (checaBotao() == "Tecladista") {
						
						textArea.setText("Classe Tecladista");
						lblNewLabel_2.setIcon(new ImageIcon("D:\\images\\tecladista.jpg"));
					}
					else if (checaBotao() == "Baterista") {
						
						textArea.setText("Classe Baterista");
						lblNewLabel_2.setIcon(new ImageIcon("D:\\images\\baterista.jpg"));
					}
				}
			}
		}
		
		private String checaBotao() {
			
			if (rdbtnNewRadioButton.getModel() == bg1.getSelection()) {
				return rdbtnNewRadioButton.getLabel();
			}
			else if (rdbtnNewRadioButton_1.getModel() == bg1.getSelection()) {
				return rdbtnNewRadioButton_1.getLabel();
			}
			else if (rdbtnNewRadioButton_2.getModel() == bg1.getSelection()) {
				return rdbtnNewRadioButton_2.getLabel();
			}
			else if (rdbtnNewRadioButton_3.getModel() == bg1.getSelection()) {
				return rdbtnNewRadioButton_3.getLabel();
			}
			else if (rdbtnNewRadioButton_4.getModel() == bg1.getSelection()) {
				return rdbtnNewRadioButton_4.getLabel();
			}
			
			return "none";
		}
			
		/*public static void main (String[] args) {
			new GUInovojogo();
		}*/
}
